<#import "../../common/layout.ftl" as layout/>
<#import "../../common/dataTables.ftl" as dataTables/>
<#import "../../common/modern-forms.ftl" as mform/>
<#import "../../common/modal.ftl" as modal/>

<!DOCTYPE html>
<html>
<style>
    #modal-mUser .modal-body{
        max-height: 800px;
    }
    #modal-mUser .col{
        padding:15px;
    }
</style>
<@layout.head title="数据表管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">
<#--<h4 class="page-header">-->
<#--<i class="fa fa-cog fa-spin"></i>-->
<#--用户管理-->
<#--</h4>-->

    <!-- 表格 -->
    <div class="content">
        <!-- 按钮 -->
        <div id="mUserBtnGroup" class="btn-group">
            <button type="button" class="btn btn-blue btn-sm"
                    onclick="javascript:refreshData();">
                <i class="fa fa-refresh"></i> 刷新
            </button>
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:newmUser();">
                <i class="fa fa-plus"></i> 新增
            </button>
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:editmUser();">
                <i class="fa fa-edit"></i> 修改
            </button>
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:delmUser();">
                <i class="fa fa-minus"></i> 删除
            </button>

        </div>
        <!-- 表格 -->
        <@dataTables.commonTable id="tabmUser"/>

        <!-- 弹出窗 -->
        <@modal.editModal id="modal-mUser" buttonId="btnmUserSave" width="1000px">
            <@mform.container id="userEditForm">
                <@mform.row>
                    <#list tableColumList as item>
                        <@mform.input_text id="${item.COLUMN_NAME}" name="${item.COLUMN_NAME}" size=6
                        label="${item.COLUMN_NAME}" placeholder="${item.COLUMN_NAME}" icon="fa-edit" />
                    </#list>
                    <#--<@mform.input_text id="cUserName" name="cUserName" size=6-->
                    <#--label="用户名" placeholder="请输入用户名" icon="fa-edit" />-->

                </@mform.row>
            </@mform.container>
        </@modal.editModal>

    </div>
</div>
</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>

<script type="text/javascript">
    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }

    var columns =
            [
                <#list tableColumList as item>
                    {"title": "${item.COLUMN_NAME}", "data": "${item.COLUMN_NAME}"},
                </#list>
    ];

    var mUserTable = $('#tabmUser').winningTable({
        columns: columns,
        pageLength: 10,
        toolbar: "#mUserBtnGroup",
        /*select: {style: 'multi'},*/
        fixedHeader: false,
        searching: true,
        ordering: true,
        buttons: [

        ]
    });




    function refreshData() {
        mUserTable.queryDataInPage("queryData",{name:'${tableName}'});
    }
    refreshData();
    function newmUser() {
        $('#modal-mUser').find(".modal-title").text('向 ${tableName} 表添加记录');
        clearForm($('#modal-mUser'));
        $("#cPassword").parent().parent().attr("style", "display:block");
        $("#btnmUserSave").unbind();
        $("#btnmUserSave").click(savemUserNew);
        $('#modal-mUser').modal();
    }

    function editmUser() {
        var datas = mUserTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var $modal = $("#modal-mUser");
        $modal.find(".modal-title").text("修改用户信息");
        clearForm($modal);
        var data = datas[0];

        if (data.bState == true) {
            data.bState = "1";
        } else {
            data.bState = "0";
        }

        initFormItemData($("#userEditForm"), data);
        $("#cPassword").parent().parent().attr("style", "display:none");
        $("#btnmUserSave").unbind();
        $("#btnmUserSave").click(savemUserUpdate);
        $modal.modal();
        $modal.on('shown.bs.modal', function (e) {
//            initFormSelect($("#modal-mUser"), data);
        })
    }



    function delmUser() {
        var datas = mUserTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var data = datas[0];
        var cUserID = data.cUserID;
        swal({
            title: "确定删除",
            text: "是否确定删除此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if(!result.value)
                return;
            $.ajax({
                type: 'POST',
                data: 'cUserID=' + cUserID,
                url: '${request.contextPath}/usermanage/delete',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            var newRow = data.data;
                            // 删除table行
                            mUserTable.getSelectedRows().remove().draw();
                        });
                    }
                    else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    function savemUserNew() {
        // $("#cDeptName").val($("#cDeptID").find("option:selected").text());
        // $("#cPlaceName").val($("#cPlaceID").find("option:selected").text());
        // var pass = validateItemNotNull($("#cUserCode"));
        // pass = pass && validateItemNotNull($("#cUserName"));
        // pass = pass && validateItemNotNull($("#cPassword"));
        // if (!pass) {
        //     return;
        // }
        // if ($("#bState").val() == '-1') {
        //     swal("提示", "请选择用户状态", "warning");
        //     return;
        // }
        swal({
            title: "确定新增",
            text: "是否确定新增此记录",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if(!result.value)
                return;
            var requestData = $("#userEditForm form").serializeObject();
            console.log(requestData)
            $.ajax({
                type: 'POST',
                data: {datas:JSON.stringify(requestData),tableName:"${tableName}"},
                url: '${request.contextPath}/tablemanage/save',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                                    title: "成功",
                                    text: "操作成功!",
                                    type: "success",
                                    showCancelButton: false,
                                    confirmButtonText: "确定"
                                }).then(function (result) {
                                    var newRow = data.data;
                                    var row = mUserTable.dataTables.row.add(newRow).draw().node();
                                    $(row).insertBefore(mUserTable.dataTables.rows().nodes()[0]);
                                    $("#modal-mUser").modal('hide');
                                });
                    }
                    else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    function savemUserUpdate() {

        swal({
            title: "确定修改",
            text: "是否确定修改此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (result.value) {
                var requestData = $("#userEditForm form").serializeObject();
                $.ajax({
                    type: 'POST',
                    data: {datas:JSON.stringify(requestData),tableName:"${tableName}"},
                    url: '${request.contextPath}/tablemanage/update',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-mUser").modal('hide');
                                refreshData();
                            });
                        }
                        else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            }
        });
    }

</script>
</@layout.foot>
</body>
</html>